package com.fenghongzhang.day010;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private EditText input;
    private Button send;
    private WebView web;
    private Button forward;
    private Button back;
    private Button refresh;
    private Button stop;
    private ProgressBar bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        input = (EditText) findViewById(R.id.input);
        send = (Button) findViewById(R.id.send);
        web = (WebView) findViewById(R.id.web);

        bar = (ProgressBar) findViewById(R.id.bar);


        forward = (Button) findViewById(R.id.forward);
        back = (Button) findViewById(R.id.back);
        refresh = (Button) findViewById(R.id.refresh);
        stop = (Button) findViewById(R.id.stop);


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = input.getText().toString();
                //加载网页
                web.loadUrl("http://www.baidu.com");

               //加载一个本地文件 .html
//                web.loadUrl("file:///android_asset/index.html");
                String s = "<html>\n" +
                        "<head>\n" +
                        "    <title>标题</title>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "<h1>\n" +
                        "    鲁迅说 我寄你的信 总要送往邮局 不喜欢放在信箱里 我总怀疑那里会慢一些\n" +
                        "</h1>\n" +
                        "</body>\n" +
                        "</html>";

                //加载一个字符串类型
//                web.loadData(s,"text/html",null);
//                web.loadDataWithBaseURL(null,s,"text/html","utf-8",null);



                //设置在当前的app中打开
                web.setWebViewClient(new WebViewClient(){

                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
                        bar.setVisibility(View.VISIBLE);
                        Toast.makeText(MainActivity.this, "开始加载", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        bar.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, "加载完毕", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                        super.onReceivedError(view, request, error);
                        Toast.makeText(MainActivity.this, "加载出错", Toast.LENGTH_SHORT).show();
                    }
                });


                //webview的浏览器客户端 --可以更新进度
                web.setWebChromeClient(new WebChromeClient(){
                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        super.onProgressChanged(view, newProgress);
                        //给进度条赋值
                        bar.setProgress(newProgress);
                    }
                });
            }
        });



        //设置webview 参数
        WebSettings settings = web.getSettings();
        //设置支持js
        settings.setJavaScriptEnabled(true);
        //设置js可以打开新窗口
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        //设置WebView是否使用viewport
        settings.setUseWideViewPort(true);
        //设置WebView是否使用预览模式加载界面
        settings.setLoadWithOverviewMode(true);
//        //设置WebView底层的布局算法，将会重新生成WebView布局
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);



        //前进
        forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(web.canGoForward()){ //能不能后退
                    web.goForward();
                }
            }
        });

        //后退
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(web.canGoBack()){
                    web.goBack();
                }
            }
        });

        //刷新
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                web.reload();
            }
        });

        //停止
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                web.stopLoading();
            }
        });

//
//        web.clearHistory();
//        web.clearCache(true);

    }
}
